package day52.org.controller;

import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.org.domain.Dictionarytype;
import day52.org.query.DictionarytypeQuery;
import day52.org.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//Restful风格
//@GetMapping      简单查询
//@PostMapping     高级查询
//@PutMapping      新增更新数据
//@DeleteMapping   删除数据
//@PatchMapping    批量操作


@RestController
@RequestMapping("/dictype")
@Api(tags = "字典类型接口")
public class DictionarytypeController {
    @Autowired
    private IDictionarytypeService dictionarytypeService;

    @ApiOperation(value = "字典类型查找",notes = "根据字典类型id查找")
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable Long id){
        return dictionarytypeService.findOne(id);
    }

    @ApiOperation(value = "字典类型查找",notes = "查找所有字典类型")
    @GetMapping
    public List<Dictionarytype> findAll(){
       return dictionarytypeService.findAll();
    }

    @ApiOperation(value = "字典类型添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult save(@RequestBody @Valid Dictionarytype dictionarytype){
        if(dictionarytype.getId() == null){
            dictionarytypeService.add(dictionarytype);
        }else{
            dictionarytypeService.update(dictionarytype);
            System.out.println(1);
        }
        return AjaxResult.me();
    }

    @ApiOperation(value = "字典类型删除",notes = "根据字典类型id进行删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionarytypeService.delete(id);
        return AjaxResult.me();
    }
    @ApiOperation(value = "高级查询",notes = "进行分页等高级查询")
    @PostMapping
    public PageList<Dictionarytype> pageQuery(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.pageQuery(dictionarytypeQuery);
    }

    @ApiOperation(value = "批量删除",notes = "根据字典类型id进行删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        dictionarytypeService.patchDelete(ids);
        return AjaxResult.me();
    }

}
